# -*- encoding: utf-8 -*-
"""
@Author: cuiyonghua
@CreateDate: 2020/12/30 10:37 上午
@File: 第二个案例.py
@Description: 
"""
from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel  # 借助 Pydantic 来使用标准的 Python 类型声明请求体

app = FastAPI()


class Item(BaseModel):
    name: str
    price: float
    is_offer: Optional[bool] = None


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}


@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)
